// Package issues impliments logging.
package issues

import (
	"bytes"
	"log"
	"os"
	"path"

	"code.google.com/awwwe-browser/data"
)

var logbuffer bytes.Buffer
var logpath string
var Logger *log.Logger

func init() {
	// setup the awwwelogger
	var cwd string
	var err error
	cwd, err = os.Getwd()
	if err == nil {
		logpath = path.Join(cwd, "log.txt")
		Logger = log.New(&logbuffer, "Logger: ", log.Ltime|log.Lshortfile)
		Logger.Print("Log Started:")
	} else {
		logpath = data.EmptyString
		Logger = nil
	}
}

func LogReset() {
	logbuffer.Reset()
	Logger.Print("Log Restarted:")
}

func LogFlush() {
	var err error
	var file *os.File
	Logger.Print("Log Flushed:")
	if len(logpath) > 0 {
		// file, err = os.Create(logpath)
		file, err = os.OpenFile(logpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
		defer file.Close()
		if err == nil {
			file.Write(logbuffer.Bytes())
			logbuffer.Reset()
			file.Sync()
		}
	}
}
